package com.cs543.j.client.ui;

import com.cs543.j.client.cloudService.LocalClient;
import com.cs543.j.client.cloudService.filesystem.Entry;
import com.cs543.j.client.cloudService.filesystem.File;
import com.cs543.j.client.cloudService.storage.CloudStorage;
import com.cs543.j.client.cloudService.storage.Storage;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;

public class FileActionDialog extends android.support.v4.app.DialogFragment {

	CharSequence[] actionList;
	int selection;
	Entry selectedFile;
	StorageExplorer explorer;

	public FileActionDialog(CharSequence[] actionList, Entry selectedFile,StorageExplorer explorer) {
		super();
		this.actionList = actionList;
		this.selectedFile = selectedFile;
		this.explorer = explorer;
		selection = -1;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setItems(actionList, new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				selection = which;
				Intent intent;
				switch ( selection ) {
				case 0://download
					intent = new Intent(explorer, StorageExplorer.class);
					intent.putExtra(StorageExplorer.INTENT_KEY_IS_LOCAL_SD_STORAGE, true);
					intent.putExtra(StorageExplorer.INTENT_KEY_DIRECTORY_SELECTION, true);
					intent.putExtra(StorageExplorer.INTENT_KEY_FILE_SELECTION, false);
					
					explorer.setLocalRequestCode(StorageExplorer.REQUEST_SELECTION_DOWNLOAD) ;					
					explorer.startActivityForResult(intent, StorageExplorer.REQUEST_SELECTION);
					
					break;
				case 1: //copy
					intent = new Intent(explorer, ClientListViewer.class);
					intent.putExtra(StorageExplorer.INTENT_KEY_IS_LOCAL_SD_STORAGE, false);
					intent.putExtra(StorageExplorer.INTENT_KEY_DIRECTORY_SELECTION, true);
					intent.putExtra(StorageExplorer.INTENT_KEY_FILE_SELECTION, false);
					intent.putExtra(ClientListViewer.INTENT_KEY_CLOUD_STORAGE_SELECTION, true);
					
					explorer.setLocalRequestCode(StorageExplorer.REQUEST_SELECTION_COPY) ;					
					explorer.startActivityForResult(intent, StorageExplorer.REQUEST_SELECTION);
					break;
				case 2://move
					intent = new Intent(explorer, ClientListViewer.class);
					intent.putExtra(StorageExplorer.INTENT_KEY_IS_LOCAL_SD_STORAGE, false);
					intent.putExtra(StorageExplorer.INTENT_KEY_DIRECTORY_SELECTION, true);
					intent.putExtra(StorageExplorer.INTENT_KEY_FILE_SELECTION, false);
					intent.putExtra(ClientListViewer.INTENT_KEY_CLOUD_STORAGE_SELECTION, true);
					
					explorer.setLocalRequestCode(StorageExplorer.REQUEST_SELECTION_MOVE) ;					
					explorer.startActivityForResult(intent, StorageExplorer.REQUEST_SELECTION);
					break;
				case 3://rename
					RenameActionDialog renameDialog = new RenameActionDialog(selectedFile,explorer.getStorage());
					renameDialog.show(getFragmentManager(), "Rename");
					break;
				case 4: //delete
					((CloudStorage)explorer.getStorage()).delete(selectedFile);
					break;
				default:
												
				}
				

			}
		}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
			}
		});

		return builder.create();
	}

	public int getSelection() {
		return selection;
	}

}
